import ComponentBase from "../engine/ComponentBase";
import DateUtil from "../engine/DateUtil";
import { Logger } from "../engine/Logger";
import { RemoteAsset, RemoteAssetManager } from "../engine/RemoteAssetManager";
import ResourcesManager from "../engine/ResourceManager";
import { SpriteFrameComponent } from "../engine/SpriteFrameComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HeadPrefab extends ComponentBase {

    private headIcon: cc.Sprite;
    private vip: cc.Node
    private url: string = "test-1"


    onEnable(): void {
        super.onEnable()



    }

    onDisable(): void {
        super.onDisable()
    }

    public setHead(url: string, lv: number = 0) {
        if (!this.vip) {
            this.vip = this.findChild(this.node, "vip")
            if (this.vip) {
                this.vip.active = false
            }
        }
        if (!this.headIcon) {
            this.headIcon = this.getChild(this.node, "headContainer/headIcon", cc.Sprite);
            this.headIcon.node.width = 75
            this.headIcon.node.height = 75
        }
        // Logger.log("refreshPlayerList=======setHead===", url)
        if (url && url != "") {
            if (this.url != url) {
                this.url = url
                this.headIcon.spriteFrame = null
                ResourcesManager.instance.loadHttpSprite(url, this.headIcon, (isSuc: boolean, texture: cc.Texture2D) => {
                    if (isSuc) {
                    } else {
                        this.headIcon.spriteFrame = this.headIcon.getComponent(SpriteFrameComponent).spriteFrameList[0]
                    }
                })
            } else {
                let remoteAsset: RemoteAsset = RemoteAssetManager.instance.cacheAsset.get(url)
                if (remoteAsset) {
                    remoteAsset.time = Date.now()
                }
            }
        }
        else {
            this.url = url
            this.headIcon.spriteFrame = this.headIcon.getComponent(SpriteFrameComponent).spriteFrameList[0]
        }
    }

}
